package com.tbit.uqbike.webmanager.controller;


import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.object.pojo.dto.BatteryModelChartDTO;
import com.tbit.uqbike.object.pojo.dto.BatteryModelChartUpdateDTO;
import com.tbit.uqbike.object.pojo.vo.BatteryModelChartVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BatteryModelChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 电池型号管理
 *
 * @Author:BUGTian
 * @DATE: 2022/5/10
 */
@RestController
@RequestMapping("/batteryModelChart")
public class BatteryModelChartController {

    @Autowired
    private BatteryModelChartService batteryModelChartService;


    /**
     * 添加电池型号
     *
     * @param batteryModelChartDTO 电池型号信息
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody BatteryModelChartDTO batteryModelChartDTO, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        if (StrUtil.isNotEmpty(batteryModelChartDTO.getBatteryBrand()) && batteryModelChartDTO.getBatteryBrand().length() > 50){
            return Result.error("电池品牌名称过长");
        }else if (StrUtil.isNotEmpty(batteryModelChartDTO.getBatteryModel()) && batteryModelChartDTO.getBatteryModel().length() > 50){
            return Result.error("电池型号名称过长");
        }
        return batteryModelChartService.insert(batteryModelChartDTO, login);
    }


    /**
     * 分页查询电池型号信息
     *
     * @param page       当前页码
     * @param size       每页显示的行
     * @param accountIds 品牌id，多个用,隔开
     * @return
     */
    @GetMapping("/queryPage")
    public Result<PageResult<BatteryModelChartVO>> queryPage(@NotNull Integer page, @NotNull Integer size, Integer... accountIds) {
        if (page == null || size == null) {
            return Result.error("分页参数有误!");
        }
        if (accountIds == null || accountIds.length < 1) {
            return Result.error("请选择品牌！");
        }
        PageResult<BatteryModelChartVO> pageResult = batteryModelChartService.queryPage(page, size, accountIds);
        return Result.success(pageResult);
    }

    /**
     * 更新电池型号信息
     *
     * @param batteryModelChartUpdateDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody BatteryModelChartUpdateDTO batteryModelChartUpdateDTO, HttpServletRequest request) {
        if (batteryModelChartUpdateDTO.getBatteryModelId() == null) {
            return Result.error("电池型号id不能为空");
        }
        if (StrUtil.isNotEmpty(batteryModelChartUpdateDTO.getBatteryBrand()) && batteryModelChartUpdateDTO.getBatteryBrand().length() > 50){
            return Result.error("电池品牌名称过长");
        }else if (StrUtil.isNotEmpty(batteryModelChartUpdateDTO.getBatteryModel()) && batteryModelChartUpdateDTO.getBatteryModel().length() > 50){
            return Result.error("电池型号名称过长");
        }
        LoginInfo loginInfo = BaseController.getLogin(request);
        return batteryModelChartService.update(batteryModelChartUpdateDTO, loginInfo);
    }

    /**
     * 删除电池型号信息
     *
     * @param batteryModelIds 电池型号id，多个用,隔开
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@NotNull Integer... batteryModelIds) {
        return batteryModelChartService.delete(batteryModelIds);
    }


}
